/*
  ==============================================================================

	PluginChainPool.h
	Created: 28 Nov 2024 11:43:34am
	Author:  leis

  ==============================================================================
*/

#pragma once
#include <vector>
#include <queue>
#include <thread>
#include <chrono>
#include <atomic>
#include "PluginChain.h"
class PluginChainPool
{
public:
	static PluginChainPool& getInstance()
	{
		static PluginChainPool instance;
		return instance;
	}
	PluginChainPool(const PluginChainPool&) = delete;
	PluginChainPool& operator=(const PluginChainPool&) = delete;
	void Init(int initInstanceNum, int maxInstanceNum,int bufferSize =512,int sampleRate=44100,bool realTime=false);
	PluginChain* GetAvaliableChain();
	void MarkChainAsAvailable(int index);
	bool CheckChainAvailable();
	PluginChain* GetRealTimeChain();
	void Exit();
private:
	PluginChainPool();
	~PluginChainPool();
	std::vector<PluginChain*> pluginChains;
	std::queue<int> availableChains;
	std::vector<char*> filesNeedProcess;
	std::atomic<int> numInstance = 0;
	std::mutex mtx;
	int maxInstance = 10;
	int initInstance = 1;
	int sampleRate = 44100;
	int bufferSize = 512;
	bool realTime = false;
	void AddInstance(int index);
	void InnerInit();
	std::thread initThread;
};